package com.wedo.mall.activity.user;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.e.library.activity.EActivity;
import com.e.library.http.EApi;
import com.e.library.http.EClient;
import com.e.library.http.EObserver;
import com.e.library.http.EResponse;
import com.e.library.utils.EStack;
import com.e.library.widget.ETitleBar;
import com.wedo.mall.R;
import com.wedo.mall.entity.MessageDetail;
import com.wedo.mall.widget.EWebView;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import io.reactivex.Observable;

public class MessageDetailActivity extends EActivity {

    public static final String MESSAGE_ID = "id";

    private EWebView eWebView;
    @BindView(R.id.ll_web) LinearLayout ll_web;
    @BindView(R.id.title_bar) ETitleBar title_bar;

    public static void toDetail(EActivity activity,String id){
        Bundle bundle = new Bundle();
        bundle.putString(MESSAGE_ID,id);
        activity.toActivity(MessageDetailActivity.class,bundle);
    }

    @Override
    protected int layout() {
        return R.layout.activity_message_detail;
    }

    @Override
    protected void onActivityCreated(Bundle state) {
        eWebView = new EWebView(ll_web);
        getMessageDetail();

        title_bar.setOnBackwarListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EStack.onBackPressed(MessageDetailActivity.this,SystemMessageActivity.class.getName());
                defaultFinish();
            }
        });

    }

    @Override
    protected void onPause() {
        super.onPause();
        isCorE();
    }

    private void getMessageDetail(){
        final String id = getIntent().getStringExtra(MESSAGE_ID);
        loading();
        Map<String,String> params = new HashMap<>(1);
        params.put(MESSAGE_ID,id);
        Observable<EResponse> observable = EClient.with().api().post(EApi.USER_MESSAGEDETAIL,params);
        EClient.with().observer(observable, new EObserver() {
            @Override
            public void successful(EResponse response) {
                MessageDetail detail = response.object("list",MessageDetail.class);
                eWebView.data(detail.content);
                dismiss();
            }

            @Override
            public void failure(String msg) {
                dismiss(msg);
            }
        });
    }

    @Override
    protected void onDestroy() {
        eWebView.onDestroy();
        super.onDestroy();
    }

    @Override
    public void onBackPressed() {
        EStack.onBackPressed(this,SystemMessageActivity.class.getName());
        super.onBackPressed();
    }
}
